package org.tobuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.tobuy.vo.*;
import org.tobuy.factory.*;

public class OfferPrice extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path="index.jsp?action=offerPriceResult";
		String info = "Failed!Please notice the current offer price and the exp. time of this auction! And U cant do a self Auction!";
		String item_id = request.getParameter("item_id");
		double new_offer = Double.parseDouble(request.getParameter("newoffer"));
		System.out.print(new_offer);
		HttpSession session = request.getSession();
		User buyer = (User)session.getAttribute("user");
		Auction auction= new Auction();
		try {
			auction = AuctionDAOFactory.getIAuctionDAOInstance().findAuction(item_id);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		Timestamp now = new Timestamp(System.currentTimeMillis());
		if(now.before(auction.getExp_time())&&new_offer>auction.getBuyer_offer()&&!buyer.getUser_id().equals(auction.getSeller_id())){
			try {
				if(AuctionDAOFactory.getIAuctionDAOInstance().updateAuction(item_id, new_offer , buyer.getUser_id())){
					info = "Success!";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("info", info);
		request.getRequestDispatcher(path).forward(request, response);

	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
